package com.zhiyou100.javaweb.myservlet.day005;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @packageName: ${NAME}
 * @className: ${PACKAGE_NAME}
 * @Description: TODO cookie练习
 * @author: yang
 * @date: 2020/5/27
 */
@WebServlet(name = "Day004ServletCookie",urlPatterns = "/day005/cookieDemo1")
public class Day005ServletCookie extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 1 创建Cookie对象
        Cookie c1 = new Cookie("c1", "c1_value");
        Cookie c2 = new Cookie("c2", "c2_value");

        // 2 发送Cookie 通过response的addCookies的方法把创建cookie添加到响应头
        response.addCookie(c1);
        response.addCookie(c2);

        HttpSession session = request.getSession();
        Enumeration<String> attributeNames = session.getAttributeNames();
        Object name = session.getAttribute("name");
        session.removeAttribute("name");
        session.setAttribute("name","name");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
